package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.*;
import com.monitor.entity.*;
import com.monitor.service.ErpSaleRepairOrderService;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 销售维修表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-12
 */
@Service
@Transactional
public class ErpSaleRepairOrderServiceImpl extends ServiceImpl<ErpSaleRepairOrderMapper, ErpSaleRepairOrder> implements ErpSaleRepairOrderService {

	@Resource
	private ErpCustomerMapper erpCustomerMapper;
	
	@Resource
	private ErpContractMapper erpContractMapper;
	
	@Resource
	private ErpContractCustomerMapper erpContractCustomerMapper;
	
	@Resource
	private ErpContractRepairMapper erpContractRepairMapper;
	
	@Resource
	private ErpSaleRepairOrderMapper erpSaleRepairOrderMapper;
	
	@Resource
	private ErpSaleRepairSaleMapper erpSaleRepairSaleMapper;
	
	@Resource
	private ErpMaterielFlowMapper erpMaterielFlowMapper;
	
	@Resource
	private ErpSaleRepairRepairMapper erpSaleRepairRepairMapper;
	
	@Resource
	private ErpSaleRepairGarageMapper erpSaleRepairGarageMapper;
	
	@Resource
	private ErpInspectionContentMapper erpInspectionContentMapper;
	
	@Resource
	private ErpInspectionOrderMapper erpInspectionOrderMapper;
	
	@Resource
	private ErpCustomerWarehouseMapper erpCustomerWarehouseMapper;
	
	@Resource
	private ErpMaterielFlowSummaryMapper erpMaterielFlowSummaryMapper;
	
	@Resource
	private ErpWarehouseMapper erpWarehouseMapper;
	
	@Resource
	private ErpCustomerCarMapper erpCustomerCarMapper;
	
	@Resource
	private ErpRefuelingMapper erpRefuelingMapper;
	
	@Resource
	private ErpCarTireArchivesMapper erpCarTireArchivesMapper;
	
	@Resource
	private ErpMaterielMapper erpMaterielMapper;
	
	@Resource
	private ErpTireArchivesWorkMapper erpTireArchivesWorkMapper;
	
	@Resource
	private ErpParametersMapper erpParametersMapper;
	
	@Resource
	private ErpCustomerLimitMapper erpCustomerLimitMapper;
	
	@Resource
	private ErpTirePoliceMapper erpTirePoliceMapper;
	
	@Override
	public Result<Map<String, Object>> findMon(Integer custId, String workDtS, String workDtE) {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<>();
		Purchase pur = new Purchase();
		pur.setPrice(0);
		pur.setTireCount(0);
		pur.setTirePrice(0);
		pur.setRimCount(0);
		pur.setRimPrice(0);
		pur.setGasolineCount(0);
		pur.setGasolinePrice(0);
		pur.setDieselCount(0);
		pur.setDieselPrice(0);
		
		Mileage mil = new Mileage();
		mil.setCarPrice(0);
		mil.setOperateCar(0);
		mil.setMileage(0);// 运营里程
		mil.setFuel(0);// 燃油费用
		mil.setRepair(0);// 维修费用
		mil.setOil(0);// 油耗/百公里
		mil.setTire(0);// 轮胎/百公里
		mil.setComprehensive(0);// 综合/百公里
		
		TireSituation tir = new TireSituation();
		tir.setTireCount(0);// 轮胎总数
		tir.setMileage(0);// 里程达限
		tir.setTread(0);// 胎纹达限
		tir.setAccident(0);// 意外损耗
		tir.setPressureH(0);// 胎压超高限
		tir.setPressure(0);// 胎压正常
		tir.setPressureL(0);// 胎压超低限
		tir.setTemperature(0);// 胎温正常
		tir.setTemperatureH(0);// 胎温超高
		
		TreadSituation tre = new TreadSituation();
		tre.setEleven(0);// >11
		tre.setFive(0);// 5-11
		tre.setThree(0);// 2.5-5
		tre.setTwo(0);// <2.5
		tre.setFlat(0);// 平磨
		tre.setPartial(0);// 偏磨
		tre.setHeart(0);// 磨心
		tre.setScratch(0);// 划伤
		tre.setBite(0);// 啃胎
		
		List<Purchase> listPurchase = new ArrayList<>();
		List<Replacement> listReplacement = new ArrayList<>();
		List<Repair> listRepair = new ArrayList<>();
		List<Maintenance> listMaintenance = new ArrayList<>();
		List<Replacement> listStock = new ArrayList<>();
		List<Mileage> listMileage = new ArrayList<>();
		List<TireSituation> listTireSituation = new ArrayList<>();
		List<TreadSituation> listTreadSituation = new ArrayList<>();
		List<ErpReportChild> listReportChild = new ArrayList<>();
		
		List<ErpCustomer> list1 = erpCustomerMapper.selCustomerList(custId);// 获取管辖客户列
		for(int i = 0; i < list1.size(); i++){
			ErpCustomer c =  list1.get(i);
			// 查询合同协议
			List<ErpContractRepair> Rep = this.selContractRepair(custId, workDtE);
			List<String> lists = new ArrayList<String>();
			List<String> listi = new ArrayList<String>();
			for(int j = 0; j < Rep.size(); j++){
				ErpContractRepair cr = Rep.get(j);
				lists.add(cr.getRepName());
				listi.add(cr.getRepPrice());
			}
			long startTIme = new Date().getTime();
			// 购置
			Purchase p = this.selPurchaseList(c, workDtS, workDtE);
			pur.setPrice(pur.getPrice() + p.getPrice());
			pur.setTireCount(pur.getTireCount() + p.getTireCount());
			pur.setTirePrice(pur.getTirePrice() + p.getTirePrice());
			pur.setRimCount(pur.getRimCount() + p.getRimCount());
			pur.setRimPrice(pur.getRimPrice() + p.getRimPrice());
			pur.setDieselCount(pur.getDieselCount() + p.getDieselCount());
			pur.setDieselPrice(pur.getDieselPrice() + p.getDieselPrice());
			pur.setGasolineCount(pur.getGasolineCount() + p.getGasolineCount());
			pur.setGasolinePrice(pur.getGasolinePrice() + p.getGasolinePrice());
			listPurchase.add(p);
			System.out.println("0000000000000000000000000000000-------"+(new Date().getTime()-startTIme));
			
			startTIme = new Date().getTime();
			// 更换listReplacement
			Replacement R = this.selReplacementList(c, workDtS, workDtE);
			listReplacement.add(R);
			System.out.println("1111111111111111111111111111111111-------"+(new Date().getTime()-startTIme));
			
			startTIme = new Date().getTime();
			// 维修Repair
			Repair re = this.selRepairList(custId,c, workDtS, workDtE, lists, listi);
			listRepair.add(re);
			System.out.println("222222222222222222222222222222222-----"+(new Date().getTime()-startTIme));
			
			startTIme = new Date().getTime();
			// 维护 Maintenance
			Maintenance m = this.selMaintenance(custId,c, workDtS, workDtE, lists, listi);
			listMaintenance.add(m);
			System.out.println("333333333333333333333333333333333-----"+(new Date().getTime()-startTIme));
			
			startTIme = new Date().getTime();
			// 库存 listStock
			Replacement stock = this.selStockList(custId,c, workDtS, workDtE);
			listStock.add(stock);
			System.out.println("444444444444444444444444444444444------"+(new Date().getTime()-startTIme));
			
			startTIme = new Date().getTime();
			// 运营里程/成本 Mileage
			Mileage mi = this.selMileageList(c, workDtS, workDtE);
			mil.setCarPrice(mil.getCarPrice() + mi.getCarPrice());
			mil.setOperateCar(mil.getOperateCar() + mi.getOperateCar());
			mil.setMileage(mil.getMileage() + mi.getMileage());// 运营里程
			mil.setFuel(mil.getFuel() + mi.getFuel());// 燃油费用
			mil.setRepair(mil.getRepair() + mi.getRepair());// 维修费用
			mil.setOil(mil.getOil() + mi.getOil());// 油耗/百公里
			mil.setTire(mil.getTire() + mi.getTire());// 轮胎/百公里
			mil.setComprehensive(mil.getComprehensive() + mi.getComprehensive());// 综合/百公里
			listMileage.add(mi);
			System.out.println("555555555555555555555555555555555555--------"+(new Date().getTime()-startTIme));
			
			startTIme = new Date().getTime();
			// 轮胎更新/胎压/胎温listTireSituation
			TireSituation t = this.selTireSituationList(custId,c, workDtS, workDtE);
			tir.setName(c.getCusName());
			tir.setCk(t.getCk());
			tir.setTireCount(tir.getTireCount()+t.getTireCount());// 轮胎总数
			tir.setMileage(tir.getMileage() +t.getMileage());// 里程达限
			tir.setTread(tir.getTread() +t.getTread());// 胎纹达限
			tir.setAccident(tir.getAccident()+ t.getAccident());// 意外损耗
			tir.setPressureH(tir.getPressureH() + t.getPressureH());// 胎压超高限
			tir.setPressure(tir.getPressure() + t.getPressure());// 胎压正常
			tir.setPressureL(tir.getPressureL() + t.getPressureL());// 胎压超低限
			tir.setTemperature(tir.getTemperature() + t.getTemperature());// 胎温正常
			tir.setTemperatureH(tir.getTemperatureH() + t.getTemperatureH());// 胎温超高
			listTireSituation.add(t);
			System.out.println("6666666666666666666666666666666666----------"+(new Date().getTime()-startTIme));
			
			startTIme = new Date().getTime();
			// 轮胎胎纹/状态
			TreadSituation tr = this.selTreadSituationList(c, workDtS, workDtE);
			tre.setName(c.getCusName());
			tre.setEleven(tre.getEleven()+tr.getEleven());// >11
			tre.setFive(tre.getFive()+tr.getFive());// 5-11
			tre.setThree(tre.getThree()+tr.getThree());// 2.5-5
			tre.setTwo(tre.getTwo()+tr.getTwo());// <2.5
			tre.setFlat(tre.getFlat()+tr.getFlat());// 平磨
			tre.setPartial(tre.getPartial()+tr.getPartial());// 偏磨
			tre.setHeart(tre.getHeart()+tr.getHeart());// 磨心
			tre.setScratch(tre.getScratch()+tr.getScratch());// 划伤
			tre.setBite(tre.getBite()+tr.getBite());// 啃胎
			listTreadSituation.add(tr);
			System.out.println("777777777777777777777777777777777777-----------"+(new Date().getTime()-startTIme));
			
			startTIme = new Date().getTime();
			// 更新/购置rep
			ErpReportChild r = new ErpReportChild();
			r.setRepName(c.getCusName());
			listReportChild.add(r);
			System.out.println("88888888888888888888888888888888888888-----------"+(new Date().getTime()-startTIme));
		}
		map.put("listPurchase", listPurchase);
		map.put("listReplacement", listReplacement);
		map.put("listRepair", listRepair);
		map.put("listMaintenance", listMaintenance);
		map.put("listStock", listStock);
		map.put("listMileage", listMileage);
		map.put("listTireSituation", listTireSituation);
		map.put("listTreadSituation", listTreadSituation);
		map.put("listReportChild", listReportChild);
		return new Result<Map<String,Object>>(map, map.size());
	}

		
		/**
		 * 查询合同协议
		 * 
		 * @param custId
		 * @return List<Map>
		 */
		public List<ErpContractRepair> selContractRepair(int custId, String workDtE){
			ErpCustomer cus = erpCustomerMapper.selectById(custId);
			List<ErpContract> con= erpContractMapper.selectList(new EntityWrapper<ErpContract>().eq("customer_id", cus.getId()));
			String conIds = "-1";
			for(ErpContract co :con){
				conIds +=","+co.getId();
			}
			List<ErpContractCustomer> cc =erpContractCustomerMapper.selectByCustIds(conIds, workDtE, cus.getCusName());
			String Ids = "-1";
			for(ErpContractCustomer co :cc){
				Ids +=","+co.getContractId();
			}
			return erpContractRepairMapper.selectByCustIds(Ids);
		}
		
		/**
		 * 查询客户工作报告购置列
		 * 
		 * @param c
		 * @param workDtS
		 * @param workDtE
		 * @return Purchase
		 */
		public Purchase selPurchaseList(ErpCustomer c, String workDtS, String workDtE){
			Purchase p = new Purchase();
			p.setName(c.getCusName());
			p.setPrice(0);
			p.setTireCount(0);
			p.setTirePrice(0);
			p.setRimCount(0);
			p.setRimPrice(0);
			p.setGasolineCount(0);
			p.setGasolinePrice(0);
			p.setDieselCount(0);
			p.setDieselPrice(0);
			List<Integer>  sroIds = erpSaleRepairOrderMapper.selectByCustomerName(c.getId(), workDtS, workDtE);
			String sroId = "a";
			for(Integer s : sroIds){
				sroId+=","+s;
			}
			Map<String,Object> a = new HashMap<>();
			if(!sroId.equals("a")){
				String str2 = sroId.substring(1, sroId.length());
				String str3 = str2.substring(1, str2.length());
				List<Map<String,Object>> list = erpSaleRepairSaleMapper.selectBySroIds(str3);
				
				for(int j = 0; j < list.size(); j++){
					a = list.get(j);
					p.setPrice(p.getPrice() + Double.parseDouble(a.get("a0").toString()));
					if(a.get("a2").toString().equals("轮胎")){
						p.setTireCount(Integer.valueOf(a.get("a0").toString()));
						p.setTirePrice(Double.parseDouble(a.get("a1").toString()));
					}else if(a.get("a2").toString().equals("轮辋")){
						p.setRimCount(Integer.valueOf(a.get("a0").toString()));
						p.setRimPrice(Double.parseDouble(a.get("a1").toString().toString()));
					}else if(a.get("a2").toString().equals("柴油")){
						p.setDieselCount(Integer.valueOf(a.get("a0").toString()));
						p.setDieselPrice(Double.parseDouble(a.get("a1").toString().toString()));
					}else{
						p.setGasolineCount(Integer.valueOf(a.get("a0").toString()));
						p.setGasolinePrice(Double.parseDouble(a.get("a1").toString().toString()));
					}
				}
			}else{
				p.setTireCount(0);
				p.setTirePrice(0);
				p.setRimCount(0);
				p.setRimPrice(0);
				p.setDieselCount(0);
				p.setDieselPrice(0);
				p.setGasolineCount(0);
				p.setGasolinePrice(0);	
			}
			List<Map<String,Object>>  matFlowIds = erpMaterielFlowMapper.selectByCustId(workDtS, workDtE, c.getId());
			String matFlowId  = "a";
			List<Integer> list=new ArrayList<>();
			for (int i = 0; i < matFlowIds.size(); i++) {
				Map<String, Object> map =  matFlowIds.get(i);
				list.add(Integer.parseInt((map.get("id").toString())));
			}
			for(Integer s : list){
				matFlowId +=","+s;
			}
			if(!matFlowId.equals("a")){
				String str9 = matFlowId.substring(1, matFlowId.length());
				String str10 = str9.substring(1, str9.length());
				List<Map<String,Object>> list0 = erpSaleRepairSaleMapper.selectByFlowId(str10);
				for(int j = 0; j < list0.size(); j++){
					a =  list0.get(j);
					p.setPrice(p.getPrice() + Double.parseDouble(a.get("a1").toString().toString()));
					if(a.get("a2").toString().equals("轮胎")){
						p.setTireCount(Integer.valueOf(a.get("a0").toString()));
						p.setTirePrice(Double.parseDouble(a.get("a1").toString().toString()));
					}else if(a.get("a2").toString().equals("轮辋")){
						p.setRimCount(Integer.valueOf(a.get("a0").toString()));
						p.setRimPrice(Double.parseDouble(a.get("a0").toString().toString()));
					}else if(a.get("a2").toString().equals("柴油")){
						p.setDieselCount(Integer.valueOf(a.get("a0").toString()));
						p.setDieselPrice(Double.parseDouble(a.get("a1").toString().toString()));
					}else{
						p.setGasolineCount(Integer.valueOf(a.get("a0").toString()));
						p.setGasolinePrice(Double.parseDouble(a.get("a1").toString().toString()));
					}
				}
			}else{
				p.setTireCount(0);
				p.setTirePrice(0);
				p.setRimCount(0);
				p.setRimPrice(0);
				p.setDieselCount(0);
				p.setDieselPrice(0);
				p.setGasolineCount(0);
				p.setGasolinePrice(0);	
			}
			return p;
		}
		
		
		/**
		 * 查询客户报告更换列
		 * 
		 * @param c
		 * @param workDtS
		 * @param workDtE
		 * @return Replacement
		 */
		public Replacement selReplacementList(ErpCustomer c, String workDtS, String workDtE){
			//String hql = "select sro.id from ErpSaleRepairOrder sro where sro.customerId = ? and sro.saleRepairDate>=? and sro.saleRepairDate<=?";
			List<Integer>  sroIds = erpSaleRepairOrderMapper.selectByCustomerName(c.getId(), workDtS, workDtE);
			String sroId = "-1";
			for(Integer s : sroIds){
				sroId+=","+s;
			}
			List<Map<String,Object>> list = erpSaleRepairRepairMapper.selectBySroId(sroId);
			Replacement R = new Replacement();
			R.setName(c.getCusName());
			R.setTire(0);
			R.setRim(0);
			R.setInner(0);
			R.setOil(0);
			R.setAirFilter(0);
			R.setSteamFilter(0);
			R.setAntifreeze(0);
			R.setGearil(0);
			R.setElsedata(0);
			Map<String,Object> a = new HashMap<>();

			for(int j = 0; j < list.size(); j++) {
				if (a.size()!=0) {
					a = list.get(j);
					if (a.get("a1").toString().indexOf("轮胎") != -1) {
						R.setTire(Integer.valueOf(a.get("a0").toString()));
					} else if (a.get("a1").toString().indexOf("轮辋") != -1) {
						R.setRim(Integer.valueOf(a.get("a0").toString()));
					} else if (a.get("a1").toString().indexOf("机油") != -1) {
						R.setOil(Integer.valueOf(a.get("a0").toString()));
					} else if (a.get("a1").toString().indexOf("内胎") != -1) {
						R.setInner(Integer.valueOf(a.get("a0").toString()));
					} else if (a.get("a1").toString().indexOf("空滤") != -1) {
						R.setAirFilter(Integer.valueOf(a.get("a0").toString()));
					} else if (a.get("a1").toString().indexOf("汽滤") != -1) {
						R.setSteamFilter(Integer.valueOf(a.get("a0").toString()));
					} else if (a.get("a1").toString().indexOf("防冻液") != -1) {
						R.setAntifreeze(Integer.valueOf(a.get("a0").toString()));
					} else if (a.get("a1").toString().indexOf("齿轮油") != -1) {
						R.setGearil(Integer.valueOf(a.get("a0").toString()));
					} else {
						R.setElsedata(Integer.valueOf(a.get("a0").toString()));
					}
				}
			}
			return R;
		}
		
		/**
		 * 查询客户报告维修列
		 * 
		 * @param c
		 * @param workDtS
		 * @param workDtE
		 * @param lists
		 * @param listi
		 * @return Repair
		 */
		public Repair selRepairList(int custId,ErpCustomer c, String workDtS, String workDtE, List<String> lists, List<String> listi){
			Repair re = new Repair();
			re.setName(c.getCusName());
			re.setPrice(0);// 总计
			re.setDynamicBalance(0);// 动平衡
			re.setTyre(0);// 外胎修补
			re.setInner(0);// 内胎修补
			re.setDisassembly(0);// 拆装
			re.setTransposition(0);// 换位
			re.setRescue(0);// 车辆维修
			re.setOther(0);
			re.setRefurbished(0);//轮胎翻新

			ErpCustomer cus =erpCustomerMapper.selectById(custId);
			List<ErpContract> con= erpContractMapper.selectList(new EntityWrapper<ErpContract>().eq("customer_id", cus.getId()));
			String conIds = "-1";
			for(ErpContract co :con){
				conIds +=","+co.getId();
			}
			Date cusDate = new Date();
			List<ErpContractCustomer> cc =erpContractCustomerMapper.selectByCustIdsAndName(conIds, c.getCusName());
			if (null!=cc){
			for(ErpContractCustomer cn :cc){
				cusDate = DataUtil.strToDateLong(cn.getContractingDate());
			}
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
			String  cdate = sdf.format(cusDate);
			
			List<ErpSaleRepairOrder>  sroIds = erpSaleRepairOrderMapper.selectBySroIds(c.getId(), workDtS, workDtE, c.getCusName());
			Integer sroId = -1;
			List<Integer> sorIdList=new ArrayList<>();
			sorIdList.add(sroId);
			for(ErpSaleRepairOrder s : sroIds){
				sorIdList.add(s.getId());
				re.setPrice(re.getPrice() + s.getSaleRepairCost());
			}
			List<ErpSaleRepairRepair> list = erpSaleRepairRepairMapper.selectList(new EntityWrapper<ErpSaleRepairRepair>().in("order_id", sorIdList));
			for(ErpSaleRepairRepair s : list){
				String repairProject = s.getRepairRepair();
				ErpSaleRepairOrder sr = erpSaleRepairOrderMapper.selectById(s.getOrderId());
				String Date = sr.getSaleRepairSheetId().substring(4, 10);
				
				
				if(repairProject.indexOf("动平衡") != -1){
					Double in = 0.0;
					if(Date.compareTo(cdate)>=0){
						in = lists.indexOf("动平衡") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("动平衡")));
					}
					re.setDynamicBalance(re.getDynamicBalance() + in);
				}
				if(repairProject.indexOf("外胎修补") != -1){
					Double in = 0.0;
					if(Date.compareTo(cdate)>=0){
						in = lists.indexOf("外胎修补") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("外胎修补")));
					}
					re.setTyre(re.getTyre() + in);
				}
				if(repairProject.indexOf("内胎修补") != -1){
					Double in = 0.0;
					if(Date.compareTo(cdate)>=0){
						in = lists.indexOf("内胎修补") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("内胎修补")));
					}
					re.setInner(re.getInner() + in);
				}
				if(repairProject.indexOf("拆装") != -1){
					Double in = 0.0;
					if(Date.compareTo(cdate)>=0){
						in = lists.indexOf("拆装") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("拆装")));
					}
					re.setDisassembly(re.getDisassembly() + in);// 无内胎拆装
				}
				if(repairProject.indexOf("轮胎换位") != -1){
					Double in = 0.0;
					if(Date.compareTo(cdate)>=0){
						in = lists.indexOf("轮胎换位") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("轮胎换位")));
					}
					re.setTransposition(re.getTransposition() + in);// 换位
				}
				if(repairProject.indexOf("轮胎翻新") != -1){
					Double in = 0.0;
					if(Date.compareTo(cdate)>=0){
						in = lists.indexOf("轮胎翻新") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("轮胎翻新")));
					}
					re.setRefurbished(re.getRefurbished() + in);// 换位
				}
			}
			
			List<ErpSaleRepairGarage> srgs = erpSaleRepairGarageMapper.selectByirderIds(sroId+"");
			for(ErpSaleRepairGarage srg:srgs){
				re.setRescue(re.getRescue()+Double.valueOf(srg.getMoney()));
			}
			re.setOther(re.getPrice() - re.getDynamicBalance() - re.getTyre() - re.getInner() - re.getDisassembly()
					- re.getRefurbished() - re.getTransposition() - re.getRescue());
			return re;
		}
		
		/**
		 * 查询客户报告维护列
		 * 
		 * @param c
		 * @param workDtS
		 * @param workDtE
		 * @param lists
		 * @param listi
		 * @return Maintenance
		 */
		public Maintenance selMaintenance(int custId,ErpCustomer c, String  workDtS, String workDtE, List<String> lists, List<String> listi){
			Maintenance m = new Maintenance();
			m.setName(c.getCusName());
			m.setPrice(0);// 合计
			m.setPressure(0);// 胎压调整
			m.setForeignBody(0);// 异物清理
			m.setSmallHole(0);// 小洞修补
			m.setChange(0);// 换芯/帽
			m.setSafety(0);// 检查评估
			m.setTread(0);// 胎纹检测
			m.setArchives(0);// 使用建议
			m.setOther(0);// 其它
			
			ErpCustomer cus =erpCustomerMapper.selectById(custId);
			List<ErpContract> con= erpContractMapper.selectList(new EntityWrapper<ErpContract>().eq("customer_id", cus.getId()));
			String conIds = "-1";
			for(ErpContract co :con){
				conIds +=","+co.getId();
			}
			Date cusDate = new Date();
			List<ErpContractCustomer> cc =erpContractCustomerMapper.selectByCustIdsAndName(conIds, c.getCusName());
			for(ErpContractCustomer cn :cc){
				cusDate = DataUtil.strToDateLong(cn.getContractingDate());
			}
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
			String  cdate = sdf.format(cusDate); 
			
			List<ErpInspectionContent> list = erpInspectionContentMapper.selectByBranch(workDtS, workDtE, c.getCusName());
			Double other = 0.0;
			for(int i = 0; i < list.size(); i++){
				ErpInspectionContent j = list.get(i);
				String formId = j.getFormId();
				String form = formId.substring(4, 10);
				if(StringUtils.isNotBlank(j.getPressureModi()) && j.getPressureModi().equals("胎压调节")){
					Double in = 0.0;
					if(form.compareTo(cdate)>=0){
						in = lists.indexOf("胎压调节") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("胎压调节")));
					}
					m.setPressure(m.getPressure() + in);
					other+=in;
					
				}
				if(StringUtils.isNotBlank(j.getCleanUp()) && j.getCleanUp().equals("异物清理")){
					Double in = 0.0;
					if(form.compareTo(cdate)>=0){
						in = lists.indexOf("异物清理") == -1 ? 0 : Double.valueOf(listi.get(lists.indexOf("异物清理")));
					}
					m.setForeignBody(m.getForeignBody() + in);
					other+=in;
				}
				if(StringUtils.isNotBlank(j.getMinorRepairs()) && !j.getMinorRepairs().equals("")){
					Double in = 0.0;
					if(form.compareTo(cdate)>=0){
					in = lists.indexOf("小洞修补") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("小洞修补")));
					}
					m.setSmallHole(m.getSmallHole() + in);
					other+=in;
				}
				if(StringUtils.isNotBlank(j.getCoreCap()) && !j.getCoreCap().equals("")){
					Double in = 0.0;
					if(form.compareTo(cdate)>=0){
					in = lists.indexOf("换气门芯") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("换气门芯")));
					}
					m.setChange(m.getChange() + in);
					other+=in;
				}
				if(StringUtils.isNotBlank(j.getTireStatus())&& !j.getTireStatus().equals("") && j.getUseDevice().equals("检查评估")){
					Double in = 0.0;
					if(form.compareTo(cdate)>=0){
						in = lists.indexOf("检查评估") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("检查评估")));
					}
					m.setSafety(m.getSafety() + in);
					other+=in;
				}
				if(StringUtils.isNotBlank(j.getTireStatus())&& !j.getTireStatus().equals("")){
					Double in = 0.0;
					if(form.compareTo(cdate)>=0){
						in = lists.indexOf("胎纹检测") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("胎纹检测"))); 
					}
					m.setTread(m.getTread() + in);
					other+=in;
				}
				if(StringUtils.isNotBlank(j.getUseDevice()) && !j.getUseDevice().equals("")){
					
					Double ins = 0.0;
					if(form.compareTo(cdate)>=0){
						ins = lists.indexOf("使用建议") == -1 ? 0 : Double.parseDouble(listi.get(lists.indexOf("使用建议")));
					}
					m.setArchives(m.getArchives() + ins);
					other+=ins;
				}
				
			}
			Double Price = erpInspectionOrderMapper.selectByBranch(workDtS, workDtE, c.getCusName());
			m.setPrice(Price == null ? 0 : Price);// 合计
			m.setOther(Price == null ? 0 : Price-other);
			
			
			
			return m;
		}
		
		/**
		 * 查询客户报告库存列
		 * 
		 * @param c
		 * @param workDtS
		 * @param workDtE
		 * @return Replacement
		 */
		public Replacement selStockList(int custId,ErpCustomer c, String workDtS,String  workDtE) {
			ErpCustomer cus = erpCustomerMapper.selectById(custId);

			Replacement r = new Replacement();
			r.setName(c.getCusName());
			List<ErpCustomerWarehouse> cws = erpCustomerWarehouseMapper.selectList(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id", c.getId()));
			if (cws.size() > 0) {
				if (cws.get(0).getCustomerId() - cus.getId() == 0) {
					r.setCk("100," + cws.get(0).getId());
				} else {
					r.setCk("1," + cws.get(0).getId());
				}
			} else {
				r.setCk("100,1");
			}
			String wareId = "-1";
			for (ErpCustomerWarehouse ware : cws) {
				wareId += "," + ware.getId();
			}
			r.setTire(0);
			r.setRim(0);
			r.setInner(0);
			r.setOil(0);
			r.setAirFilter(0);
			r.setSteamFilter(0);
			r.setAntifreeze(0);//防冻液
			r.setGearil(0);//齿轮油
			r.setElsedata(0);

			List<ErpMaterielFlowSummary> list = erpMaterielFlowSummaryMapper.selectByWareId(wareId, workDtE);
			for (ErpMaterielFlowSummary mfs : list) {
				ErpWarehouse warehouse = erpWarehouseMapper.selectById(mfs.getMaterielId());
				if (null != warehouse) {
					if (warehouse.getMaterielName().equals("轮胎")) {
						r.setTire(r.getTire() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
					} else if (warehouse.getMaterielName().equals("轮辋")) {
						r.setRim(r.getRim() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
					} else if (warehouse.getMaterielName().equals("内胎")) {
						r.setInner(r.getInner() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
					} else if (warehouse.getMaterielName().equals("机油")) {
						r.setOil(r.getOil() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
					} else if (warehouse.getMaterielName().equals("空滤")) {
						r.setAirFilter(r.getAirFilter() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
					} else if (warehouse.getMaterielName().equals("防冻液")) {
						r.setAntifreeze(r.getAntifreeze() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
					} else if (warehouse.getMaterielName().equals("齿轮油")) {
						r.setGearil(r.getGearil() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
					} else if (warehouse.getMaterielName().equals("汽滤")) {
						r.setSteamFilter(r.getSteamFilter() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
					} else {
						if (null != mfs.getMaterielStorageNumber() && null != mfs.getMaterielOutflowNumber() && null != r.getElsedata()) {
							r.setElsedata(r.getElsedata() + mfs.getMaterielStorageNumber() - mfs.getMaterielOutflowNumber());
						}
					}
				}

			}
			return r;
		}
		/**
		 * 查询客户运营里程/成本 listMileage
		 * 
		 * @param c
		 * @param workDtS
		 * @param workDtE
		 * @return Mileage
		 */
		public Mileage selMileageList(ErpCustomer c, String workDtS, String workDtE){
			Mileage mi = new Mileage();
			mi.setName(c.getCusName());
			mi.setCarPrice(0);
			mi.setOperateCar(0);// 运营车辆总数
			mi.setMileage(0);// 运营里程
			mi.setFuel(0);// 燃油费用
			mi.setRepair(0);// 维修费用
			mi.setOil(0);// 油耗/千公里
			mi.setTire(0);// 轮胎/千公里
			mi.setComprehensive(0);// //综合/百公里
			
			List<ErpCustomerCar> cars = erpCustomerCarMapper.selectList(new EntityWrapper<ErpCustomerCar>().eq("customer_id", c.getId()));
			int CarPrice = cars.size();
			mi.setCarPrice(CarPrice);
		    String plate = "'-1'";
		    for(ErpCustomerCar car:cars){
		    	plate =plate + ",'"+car.getCarPlate()+"'";
		    }
		    int OperateCar = 0;
		    Set h = new HashSet();
			List<String> pla =erpInspectionContentMapper.selectByBranchAndTime(c.getCusName(), workDtS, workDtE);
			for(String t :pla){
				if(plate.indexOf(t)>-1){
					h.add(t);
				}
			}
			OperateCar = h.size();
		    mi.setOperateCar(h.size());
		    Double mileage = 0.0;
		    if(OperateCar == 0){
		    	mi.setMileage(0);
		    	mi.setFuel(0);
		    	mi.setOil(0);
		    	mi.setRepair(0);
		    	mi.setTire(0);
		    	mi.setComprehensive(0);
		    }else{
				for(ErpCustomerCar car:cars){
					List<Map<String, Object>> inshs = erpInspectionContentMapper.selectByCustIdAndTime( car.getCarPlate(), workDtS, workDtE);
					
					if(inshs.size()>1){
						Map<String, Object> insh0 =  inshs.get(0);
						Map<String, Object> insh1 =  inshs.get(inshs.size()-1);
						Double mileage1=Double.valueOf(insh0.get("mileage").toString());
						Double mileage2=Double.valueOf(insh1.get("mileage").toString());
						mileage += mileage1-mileage2;
					}
				}
		    	
				mi.setMileage(mileage);
				double  repair = 0;
				List<ErpInspectionOrder> lit = erpInspectionOrderMapper.selectByCustName(c.getCusName(),workDtS,workDtE);
				if(lit.size()==0){
					repair = 0;
				}else{
					for(int j = 0 ; j < lit.size();j++){
						ErpInspectionOrder insh = lit.get(j);
						repair = repair+insh.getActualFee();
					}
				}
				
				//hql = "from ErpSaleRepairOrder sro where sro.customerName=? and sro.saleRepairDate>=? and sro.saleRepairDate<=?";
				List<ErpSaleRepairOrder> sros = erpSaleRepairOrderMapper.selectList(new EntityWrapper<ErpSaleRepairOrder>().eq("customer_name", c.getCusName()).between("sale_repair_date", workDtS, workDtE));
				for(ErpSaleRepairOrder sro:sros){
					if (null!= sro.getSaleRepairCost()){
					repair += sro.getSaleRepairCost();
					}
				}
				mi.setRepair(repair);
				double refuel= 0;
				List<ErpRefueling> lir = erpRefuelingMapper.selectByCustId(c.getId(),workDtS,workDtE);
				for(int j = 0 ;j<lir.size();j++){
					ErpRefueling  fef = lir.get(j);
					refuel +=fef.getRefTotal();				
				}
				if(refuel == 0.0){
					mi.setFuel(0);
					mi.setOil(0);
				}else{
					mi.setFuel(refuel);
					mi.setOil(refuel/(mileage/1000));
				}
				List list = new ArrayList();
				String[] s1 = new String[cars.size()];
				String[] s2 = new String[cars.size()];
				for(int i = 0; i < cars.size(); i++){
					s1[i] = cars.get(i).getCarPlate();
					s2[i] = cars.get(i).getCarNumber();
					List l1 = this.findTireInfo("'"+cars.get(i).getCarPlate()+"'");
					if(l1 != null){
						list.add(l1);
					}
				}
				double ltc = 0;
				double luntai = 0;
				for(int j = 0; j < list.size(); j++){
					List ss =(List)list.get(j);
					for(int k = 0; k < ss.size();k++){
						List s =(List)ss.get(k); 
						luntai = Double.parseDouble(s.get(11).toString());
						if(luntai != 0){
							ltc=  ltc +luntai;
						}
					}
				}
				DecimalFormat  df = new DecimalFormat("####0.00");
				if(ltc==0.0){
					mi.setTire(0);
				}else{
					mi.setTire(Double.parseDouble(df.format(ltc/OperateCar)));
				}
				if( mileage!=0.0){
					double  yhmkm = refuel/(mileage/1000);
					mi.setOil(Double.parseDouble(df.format(yhmkm)));
					double zh = (refuel+repair)/(mileage/1000);
					if(ltc!=0.0&&OperateCar!=0){
						zh +=Double.parseDouble(df.format(ltc/OperateCar));
					}
					mi.setComprehensive(Double.parseDouble(df.format(zh)));
				}else{
					mi.setOil(0);
					mi.setComprehensive(0);
				}
		    }
			return mi;
		}
		
		/**
		 * 查找轮胎最新消息
		 * 
		 * @param s
		 * @return
		 */
		public List findTireInfo(String s){

            s=s.replace("'","");
			ErpCustomerCar cc =erpCustomerCarMapper.selectByPlate(s);

			List<ErpCarTireArchives> ctalists = erpCarTireArchivesMapper.selectList(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cc.getId()));

			List<ErpCarTireArchives> ctalist = this.sortByTire(ctalists, s);
			List returnDatas = new ArrayList();
			for(ErpCarTireArchives cta : ctalist){
				if(cta != null){
					List returnData = new ArrayList();
					ErpCustomerCar car = erpCustomerCarMapper.selectById(cta.getCarId());
					returnData.add(car.getCarPlate());
					returnData.add(cta.getTireLocation());
					ErpMateriel materiel = erpMaterielMapper.selectById(cta.getCarId());
					if (null!=materiel) {
						returnData.add(materiel.getMaterielBrand());
						returnData.add(materiel.getMaterielSpecifications());
						returnData.add(materiel.getMaterielModel());
						returnData.add(materiel.getMaterielProductionId());
						returnData.add(cta.getTirePurchaseDate());
						returnData.add(cta.getTirePurchasePrice());
						returnData.add(cta.getTireMaintenance());
					}
					double mileage = 0.0;
					double price = 0.0, maxprice = 0.0;
					double priceRepair = 0.0;
					double logmileage=0.0;
                    if (null!=materiel) {
                        List<ErpTireArchivesWork> taws = erpTireArchivesWorkMapper.selectByTireId(materiel.getId());

                        for (ErpTireArchivesWork taw : taws) {
                            if (taw.getCalculatingMileage() == 1 && null != taw) {
                                mileage += (taw.getWorkMileage() - logmileage);
                            }
                            logmileage = taw.getWorkMileage();

                            if (taw.getCostType() == 0 && taw.getTireCost() > maxprice && null != taw) {
                                maxprice = taw.getTireCost();
                            }

                            if (taw.getCostType() == 1 && null != taw) {
                                priceRepair += taw.getTireCost();
                            }

                            if (taw.getCostType() == 2 && null != taw) {
                                price += taw.getTireCost();
                            }
                        }
                    }
					price += maxprice;
					double lines = 0.0;
					String linesS ="";
					if (null!=materiel) {
				 linesS = erpParametersMapper.selectMaxLines(materiel.getMaterielBrand(),
								materiel.getMaterielSpecifications(), materiel.getMaterielModel(),
								materiel.getMaterielPlace());
						if (StringUtils.isNotBlank(linesS)) {
							lines = Double.valueOf(linesS);
						}
					}
					returnData.add(mileage);
					returnData.add("0.0");
					returnData.add(price);
					returnData.add(lines);
					returnData.add(lines);
					returnData.add(lines);
					returnData.add(lines);
                    if (null!=materiel) {   //好像有错的
						List<ErpTireArchivesWork> tireArchivesWorks = erpTireArchivesWorkMapper.selectList(new EntityWrapper<ErpTireArchivesWork>().eq("tire_id", materiel.getId()));

					ErpTireArchivesWork taw = null;
					Timestamp workDate = null;
					for(ErpTireArchivesWork taw0:tireArchivesWorks){
						if(taw0.getWorkSheetId().indexOf("JCWH") > -1&&null!=materiel){
							if(workDate == null&&null!=materiel ){
								taw = taw0;
								workDate = new Timestamp(taw0.getWorkDate().getTime());
							}else if(taw0.getWorkDate().getTime() > workDate.getTime()){
								taw = taw0;
								workDate = new Timestamp(taw0.getWorkDate().getTime());
							}
						}
					}

					if(taw == null){
						returnData.add("");
						returnData.add("");
					}else{
						returnData.add(workDate.toString().substring(0, 10));
						returnData.add(taw.getWorkSheetId());
					}
					returnDatas.add(returnData);

				}
                }
			}
//			returnDatas.add(ctalists.get(0).getCustomerCar().getAttribute1());
			return returnDatas;
		}
		
		public List<ErpCarTireArchives> sortByTire(List<ErpCarTireArchives> ctaL, String plate){
			ErpCustomerCar cc =  erpCustomerCarMapper.selectByPlate(plate);
			String[] str = cc.getCarWheel().split("-");
			List<String> l = new ArrayList<String>();
			for(int i = 0; i < str.length; i++){
				String s1 = str[i];
				int in = Integer.parseInt(s1);
				if(in == 2){
					int num = i + 1;
					String s2 = "左" + num + "轮";
					String s3 = "右" + num + "轮";
					l.add(s2);
					l.add(s3);
				}
				if(in == 4){
					int num = i + 1;
					String s4 = "左" + num + "外";
					String s5 = "左" + num + "内";
					String s6 = "右" + num + "内";
					String s7 = "右" + num + "外";
					l.add(s4);
					l.add(s5);
					l.add(s6);
					l.add(s7);
				}
			}
			l.add("备胎");
			ErpCarTireArchives[] ctas = new ErpCarTireArchives[l.size()];

			for(ErpCarTireArchives cta : ctaL){

				int index = l.indexOf(cta.getTireLocation());

				if (null!=Integer.valueOf(index)&&null!=cta){
					if (index==-1){
						ctas[index+1] = cta;
					}else {
						ctas[index] = cta;
					}

                }

			}
			List<ErpCarTireArchives> ctaLNew = new ArrayList<ErpCarTireArchives>();
			for(ErpCarTireArchives cta : ctas){
				ctaLNew.add(cta);
			}
			return ctaLNew;
		}
		
		/**
		 *  轮胎更新/胎压/胎温listTireSituation
		 * 
		 * @param c
		 * @param workDtS
		 * @param workDtE
		 * @return Mileage
		 */
		public TireSituation selTireSituationList(int custId,ErpCustomer c, String workDtS, String workDtE){
			ErpCustomer cus = erpCustomerMapper.selectById(custId);
			
			TireSituation  ts = new TireSituation();
			ts.setName(c.getCusName());
			List<ErpCustomerWarehouse> cws = erpCustomerWarehouseMapper.selectList(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id", c.getId()));
			if(cws.size() > 0){
				if(cws.get(0).getCustomerId() - cus.getId() == 0){
					ts.setCk("100,"+cws.get(0).getId());
				}else{
					ts.setCk("1,"+cws.get(0).getId());
				}
			}else{
				ts.setCk("100,1");
			}
			String wareId= "-1";
			for(ErpCustomerWarehouse ware :cws ){
				wareId +=","+ware.getId();
			}
			ts.setTireCount(0);//轮胎总数
			ts.setMileage(0);//里程达限
			ts.setTread(0);//胎纹达限
			ts.setAccident(0);//意外损耗
			ts.setPressureH(0);//胎压超高限
			ts.setPressure(0);//胎压正常
			ts.setPressureL(0);//胎压超低限
			ts.setTemperature(0);//胎温正常
			ts.setTemperatureH(0);//胎温超高
			
			List<ErpCustomerLimit> cll = erpCustomerLimitMapper.selectList(new EntityWrapper<ErpCustomerLimit>().eq("customer_id", c.getId()));
			List<String> tires = new ArrayList<String>();
			
			for(ErpCustomerLimit cl:cll){
				String tire = cl.getLimName()+cl.getLimBrand()+cl.getLimSf()+cl.getLimModel()+cl.getLimOrigin()+cl.getLimType();
				int index = tires.indexOf(tire);
				if(index == -1){
					tires.add(tire);
				}
			}
			
			List<ErpSaleRepairOrder> lsro = erpSaleRepairOrderMapper.selectByCustId(c.getId(), workDtS, workDtE);
			String ids = "'-1'";
			for(ErpSaleRepairOrder sro :lsro){
				ids =ids + ",'"+sro.getId()+"'";
			}
			List<ErpSaleRepairRepair> lsrr = erpSaleRepairRepairMapper.selectByOrderIds(ids);
			List<String> productionIds = new ArrayList<String>();
			int mileage = 0 , tread = 0 ,accident = 0;
			for(ErpSaleRepairRepair srr:lsrr){
				int index = productionIds.indexOf(srr.getMaterielProductionId());
				if(index == -1){
					String tire = srr.getMaterielName()+srr.getMaterielBrand()+srr.getMaterielSpecifications()+srr.getMaterielModel()+srr.getMaterielPlace();
					String tireTw =tire+"剩余胎纹";
					
					index = tires.indexOf(tireTw);
					if(index == -1){
						tireTw = tire+"使用里程";
						index = tires.indexOf(tireTw);
						if(index > -1){
							Double Tiremileage = this.Tiremileage(srr.getMaterielProductionId(), workDtE);
							ErpCustomerLimit cl = cll.get(index);
							if(Tiremileage>Double.valueOf(cl.getLimReplacement())){
								mileage++;
							}
						}else{
							accident++;
						}
					}else{
						ErpCustomerLimit cl = cll.get(index);
						Double TireTread1 = srr.getTireTread1();
						Double TireTread2 = srr.getTireTread2();
						Double TireTread3 = srr.getTireTread3();
						Double TireTread4 = srr.getTireTread4();
						Double TireTread = (TireTread1+TireTread2+TireTread3+TireTread4)/4;
						if(TireTread<Double.valueOf(cl.getLimReplacement())){
							tread++;
						}
					}
				}
			}
			
			ts.setTireCount(tread+mileage+accident);
			ts.setTread(tread);
			ts.setMileage(mileage);
			ts.setAccident(accident);
			
			
			int tirePH = erpTirePoliceMapper.selecttire(c.getId(), 5, workDtS, workDtE);
			ts.setPressureH(tirePH);

			int tirePL = erpTirePoliceMapper.selecttire(c.getId(), 6, workDtS, workDtE);
			ts.setPressureL(tirePL);
			int tirePR = erpCarTireArchivesMapper.selectByCustId(c.getId());
			ts.setPressure(tirePR);

			int tireTH = erpTirePoliceMapper.selecttire(c.getId(), 3, workDtS, workDtE);
			ts.setTemperatureH(tireTH);
		
			ts.setTemperature(tirePR);
			return ts;
		}
		
		/**
		 * 轮胎胎纹/状态
		 * 
		 * @param c
		 * @param workDtS
		 * @param workDtE
		 * @return TreadSituation
		 */
		public TreadSituation selTreadSituationList(ErpCustomer c, String workDtS, String workDtE){
			TreadSituation  tp = new TreadSituation();
			tp.setName(c.getCusName());
			tp.setEleven(0);//>11
			tp.setFive(0);//5-11
			tp.setThree(0);//2.5-5
			tp.setTwo(0);//<2.5
			tp.setFlat(0);//平磨
			tp.setPartial(0);//偏磨
			tp.setHeart(0);//磨心
			tp.setScratch(0);//划伤
			tp.setBite(0);//其它
			List<ErpCustomerCar> list= erpCustomerCarMapper.selectByCode(c.getCusCode().toString());
			int eleven = 0, five = 0 ,three = 0 ,two = 0;
			int flat = 0,partial = 0,heart = 0,scratch = 0, bite = 0;
			for(ErpCustomerCar car:list){
				List<ErpInspectionContent> content = erpInspectionContentMapper.selectByCustId(car.getCarPlate(), workDtS, workDtE);
				if(content.size() > 0){
					String formid = content.get(0).getFormId();
					List<ErpInspectionContent> ics = erpInspectionContentMapper.selectList(new EntityWrapper<ErpInspectionContent>().eq("form_id", formid));
					for(ErpInspectionContent ic:ics){
						double deep1 = ic.getDeepLines1()==null?0.0:ic.getDeepLines1();
						double deep2 = ic.getDeepLines2()==null?0.0:ic.getDeepLines2();
						double deep3 = ic.getDeepLines3()==null?0.0:ic.getDeepLines3();
						double deep4 = ic.getDeepLines4()==null?0.0:ic.getDeepLines4();
						double deep = ( deep1 + deep2 + deep3 + deep4 ) / 4;
						if(deep > 11){
							eleven++;
						}else if(deep <= 11 && deep >= 5){
							five++;
						
						}else if(deep < 5 && deep >= 2.5){
							three++;
						}else{
							two++;
						}
						if(ic.getTireStatus().equals("平磨")){
							flat++;
							
						}else if(ic.getTireStatus().equals("偏磨")){
							partial++;
							
						}else if(ic.getTireStatus().equals("划伤")){
							scratch++;
							
						}else if(ic.getTireStatus().equals("磨心")){
							heart++;
						}else{
							bite++;
							
						}
					}
				}
			}
			tp.setEleven(eleven);
			tp.setFive(five);
			tp.setThree(three);
			tp.setTwo(two);
			tp.setFlat(flat);
			tp.setPartial(partial);
			tp.setHeart(heart);
			tp.setScratch(scratch);
			tp.setBite(bite);
			return tp;
		}
		
		public Double Tiremileage(String ProductionId, String date){
			ErpMateriel mat =erpMaterielMapper.selectByProductionId(ProductionId);
			List<ErpTireArchivesWork> taws =erpTireArchivesWorkMapper.selectByWorkDate(mat.getId(), date);
			Double mileage = 0.0;
			Double mileage0=0.0;
			Double mileage1=0.0;
			for(ErpTireArchivesWork taw:taws){
				mileage1 = taw.getWorkMileage();
				if(taw.getCalculatingMileage()==1){
					mileage += mileage1-mileage0;
				}
				mileage0 = taw.getWorkMileage();
			}
			return mileage;
		}
}
